<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\analysis\material;

use app\model\mongo\MongoVideoModel;
use app\model\VideoModel;
use app\service\material\VideoService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;

/**
 * 系统视频服务.
 */
final class VideoMaterialService extends MaterialService implements MaterialInterface
{
    /**
     * 获取列表数据.
     *
     * @param int $page 页数
     * @param int $limit 分页
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getList(int $page = 1, int $limit = 200)
    {
        return (new VideoModel())
            ->where('is_mark', VideoModel::MARK_NO)
            ->limit(($page - 1) * $limit, $limit)
            ->select();
    }

    /**
     * 设置视频信息.
     *
     * @param Model $model 视频模型
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function setData(Model $model): bool
    {
        if (! $model instanceof VideoModel) {
            return self::setError('未知视频模型');
        }

        if (! $model->getKey()) {
            return self::setError('未知视频模型');
        }

        $file = $this->getFile(intval($model->getAttr('file_id')));
        if (! $file->getKey()) {
            return self::setError('未知文件模型');
        }

        // 获取视频相关人员
        $allStaff = VideoService::getVideoStaff($model);
        if ($allStaff === false) {
            return false;
        }

        $areaId = VideoService::getVideoDepartmentArea($model);
        if ($areaId === false) {
            return false;
        }

        $obj = (new MongoVideoModel())
            ->where('signature', $model->getAttr('signature'))
            ->field(true)
            ->findOrEmpty();
        if (! $obj->getKey()) {
            $obj = new MongoVideoModel();
        }

        $saveData = [
            'video_id' => $model->getKey(),
            'company_id' => $model->getAttr('company_id'),
            'client_id' => $model->getAttr('client_id'),
            'department_id' => $model->getAttr('department_id'),
            'area_id' => $areaId,
            'product_id' => $model->getAttr('product_id'),
            'task_id' => $model->getAttr('task_id'),
            'subtask_id' => $model->getAttr('subtask_id'),
            'name' => $model->getAttr('name'),
            'file_id' => $model->getAttr('file_id'),
            'signature' => $model->getAttr('signature'),
            'version' => $model->getAttr('version'),
            'parent_id' => $model->getAttr('parent_id'),
            'is_abandon' => $model->getAttr('is_abandon'), // 是否弃用
            'video_target' => $model->getAttr('video_target'), // 视频类型
            'video_type' => $model->getAttr('video_type'), // 视频版式
            'is_realtime' => $model->getAttr('is_realtime'),
            'source' => $model->getAttr('task_id') > 0 ? MongoVideoModel::SOURCE_ZHICHUANG : MongoVideoModel::SOURCE_LOCAL,
            'all_staff_id' => $allStaff['all_staff_id'] ?? [],
            'creative_staff_id' => $allStaff['creative_staff_id'] ?? [],
            'creative_team_leader_id' => $allStaff['creative_team_leader_id'] ?? 0,
            'creative_department_leader_id' => $allStaff['creative_department_leader_id'] ?? 0,
            'operation_staff_id' => $allStaff['operation_id'] ?? 0,
            'zp_id' => $allStaff['zp_id'] ?? 0,
            'sj_id' => $allStaff['sj_id'] ?? 0,
            'hq_id' => $allStaff['hq_id'] ?? 0,
            'bd_id' => $allStaff['bd_id'] ?? 0,
            'ps_id' => $allStaff['ps_id'] ?? 0,
            'dy_id' => $allStaff['dy_id'] ?? 0,
            'creative_team_id' => VideoService::getVideoTaskTeam($model),
            'performer_id' => VideoService::getVideoPerformer($model),
            //            'is_use' => MongoVideoModel::USE_DISABLE,
            'create_time' => $file->getAttr('create_time'),
            'update_time' => $model->getAttr('update_time'),
        ];
        if (! $obj->getKey()) {
            $saveData['is_use'] = MongoVideoModel::USE_DISABLE;
        }
        $obj->setAttrs($saveData);
        return $obj->save();
    }

    /**
     * 根据签名获取视频模型.
     *
     * @param string $signature 签名
     * @return null|array|mixed|Model|MongoVideoModel
     */
    public static function getVideoBySignature(string $signature = '')
    {
        return (new MongoVideoModel())->where('signature', $signature)->findOrEmpty();
    }
}
